<?php
namespace Common;
use Think\Controller;
use Think\Image;
use Common\Common\Log;
abstract class BaseController extends Controller {
     //不进行权限验证的操作(控制器)
    protected $_noAuthCheck=array('Login','User');


    protected function _initialize(){
        // 检验用户权限
        if($this->checkAuth()){
            $res=B( 'Common\Behavior\AuthCheck' );
            if(!$res['success']){
                $this->failJump($res);
            }
        }
        // 加载消息提示插件
        B( 'Common\Behavior\Notice' );
    }

    /**
     * 判断是否需要进行权限验证
     */
    protected function checkAuth(){
        if(!in_array(CONTROLLER_NAME,$this->_noAuthCheck)){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 验证失败后跳转
     * @param array $res,验证失败返回信息
     */
    protected function failJump($res){
        switch($res['type']){
            case 'login':{
                $url=I('server.REQUEST_URI');
                if(stripos($url, 'admin')){
                    $url='/Admin/Login';
                }
                $this->showMsg($res['msg'],'error',$url);
                break;
            }
            case 'auth':{
                $url='/Admin/Index/errors';
                $this->showMsg($res['msg'],'error',$url);
            }
        }
    }
    /**
     * 提示消息并跳转
     * @param string $type,'success' or 'error' or 'warning'
     * @param string $msg
     * @param string $url
     * @param string $params
     */
    protected function showMsg($msg,$type='success',$url='',$params=''){
        $notice=array(
            'msg'=>$msg,
            'type'=>$type,
        );
        session('notice',$notice);

        if($url){
            $this->redirect($url,$params);
        }
    }
    /**
     *
     * @param string $type,'success' or 'error' or 'warning'
     * @param string $msg
     * @param string $url
     * @param string $params
     */
    protected function encryption($res)
    {
        return md5($res);
    }

    /**
     * 生成数据验证码
     * @param array $data
     * @param int $length
     */
    protected function createCheckDataKey($data,$length=3){
        if(is_array($data)){
            $ksortData=ksort($data);
            $encryptData=array_slice($ksortData, 0,$length);
            return md5(join('|',$encryptData).'|'.C('SITE'));
        }else{
            return false;
        }
    }
    /**
     * 上传文件裁剪
     * @param array $uploads 上传文件信息
     * @param string $width 宽
     * @param string $height 高
     * @param string $quality 质量
     */
    protected function thumb($uploads,$width='465',$height='300',$quality='99'){
        if(empty($uploads) || !is_array($uploads)){
            return ;
        }
        $imgArr=array();
        $rootPath='./Uploads/';
        foreach ($uploads as $v){
            if(!is_array($v) || !in_array($v['type'],array('image/jpeg','image/png','image/gif'))) continue;
            $imgArr[]=$rootPath.$v['savepath'].$v['savename'];
        }
        $img=new Image();
        foreach ($imgArr as $v){
            $img->open($v);
            $img->thumb($width, $height)->save($v,null,$quality);
        }
    }
    protected function getUserRole($info)
    {
        $res=M('AdminRole')->where("admin_id='%d'",$info['id'])->select();
        foreach($res as $k=>$v){
           $arr[]=$v['role_id'];
        }
        $st=implode(",",$arr);
        $role=M("Role")->where("id in (".$st.")")->select();
       return $role;
    }

}